package com.Service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.Bean.User;
import com.Common.CodeMsg;
import com.Common.PageInfo;
import com.Common.Result;
import com.Dao.UserDao;
import com.Service.UserService;
import org.junit.Test;
import java.util.*;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/4/28 17:43
 */
public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDao();
    @Override
    public Result login(String username, String password) {
        User user = userDao.selectByAccountAndPwd(username, password);
        // 当用户为空时 账号密码不正确
        if (user == null) {
            return new Result(CodeMsg.USER_ACCOUNT_PASSWORD_ERROR);
        }
        if (user.getIsDel() == 2) {
            return new Result(CodeMsg.USER_ACCOUNT_INVALID_ERROR);
        }
        return new Result(user);
    }

    @Override
    public Result queryPage(Map<String,Object> params, String page, String limit) {
        Integer pageNo =  NumberUtil.isNumber(page)?Integer.parseInt(page):1;
        Integer pageSize =  NumberUtil.isNumber(limit)?Integer.parseInt(limit):10;
        PageInfo<User> pageInfo = userDao.selectPage(params, pageNo, pageSize);
        return new Result(pageInfo);
    }

    @Override
    public Result addUser(String userName, String password, String realName, String role) {
            User user = userDao.selectByUserName(userName);
            if(user != null) {
                return new Result(CodeMsg.USER_ACCOUNT_EXISTS_ERROR);
            }
            if(!NumberUtil.isNumber(role)) {
                return new Result(CodeMsg.USER_ROLE_ERROR);
            }
            if(Integer.parseInt(role) != 1 && Integer.parseInt(role) != 2){
                return new Result(CodeMsg.USER_ROLE_ERROR);
            }
            boolean flag = userDao.insertUser(userName, password, realName, Integer.parseInt(role));
            if(!flag ) {
                return new Result(CodeMsg.USER_ADD_ERROR);
            }
            return new Result();
    }

    @Override
    public Result reset(String id) {
        if(!NumberUtil.isNumber(id)) {
            return new Result(CodeMsg.USER_USERID_ERROR);
        }
        boolean flag = userDao.resetPassword(Integer.parseInt(id));
        return flag?new Result():new Result(CodeMsg.USER_PASSWORD_RESET_ERROR);
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("realName","李");
        Result result = userService.queryPage(params, null, null);
        PageInfo data = (PageInfo)result.getData();
        List list = data.getList();
        System.out.println(data.getCount());
        Iterator<User> iterator = list.iterator();

        while (iterator.hasNext()){
            User next = iterator.next();
            System.out.println(next);
        }
    }

    @Override
    public Result deleteUser(String... ids) {
        boolean flag = userDao.batchDelete(ids);
        return flag?new Result():new Result(CodeMsg.USER_DELETE_ERROR);
    }

//    @Override
//    public Result updateImg(Integer id, String imgUrl) {
//        boolean flag = userDao.updateImg(id, imgUrl);
//        return flag?new Result():new Result(CodeMsg.USER_IMG_UPDATE_ERROR);
//    }

//    @Override
//    public Result updatePwd(Integer id, String password, String password1, String password2) {
//        //校验新密码是否一致
//        if(!StrUtil.equals(password1, password2)) {
//            return new Result(CodeMsg.USER_PASSWORD_NOT_SAME_ERROR);
//        }
//        User user = userDao.selectById(id);
//        //校验用户是否存在
//        if(user == null) {
//            return new Result(CodeMsg.USER_USERID_ERROR);
//        }
//        //校验当前密码是否正确
//        if(!StrUtil.equals(user.getPassword(), password)) {
//            return new Result(CodeMsg.USER_PASSWORD_ERROR);
//        }
//        boolean flag = userDao.updatePwd(id, password2);
//        return flag?new Result():new Result(CodeMsg.USER_PASSWORD_UPDATE_ERROR);
//    }
}
